package com.lh.java.basic.exception;

import java.io.UnsupportedEncodingException;

public class TestChar {
	public String toString() {
		return super.toString();
	}
	Object a;
	public static boolean isLetter(char c) {
		int k = 0X80;
		return c / k == 0 ? true : false;

	}

	public static int lengths(String strSrc) {
		if (strSrc == null) {
			return 0;
		}
		int len = 0;
		char[] strChar = strSrc.toCharArray();
		for (int i = 0; i < strChar.length; i++) {
			len++;
			if (!isLetter(strChar[i]))
				len++;
		}
		return len;
	}

	public static String subString(String origin, int len) {
		System.out.println(origin.length());
		if (origin == null || origin.equals("") || len < 1) {
			return "";
		}
		if (len > lengths(origin)) {
			return origin;
		}
		byte[] strByte = new byte[len];
		System.arraycopy(origin.getBytes(), 0, strByte, 0, len);
		int count = 0;
		for (int i = 0; i < len; i++) {
			int value = (int) strByte[i];
			if (value < 0)
				count++;
		}
		if (count % 2 != 0) {
			len = (len == 1) ? ++len : --len;
		}
		return new String(strByte, 0, len);

	}

	public static void main(String[] args) throws UnsupportedEncodingException {
//		System.out.println("" + subString("我ABC汉DEF", 4));
//		System.out.println("我ABC汉DEF".substring(0,1));
//		aa();
//		 String str = "我a爱中华abc我爱传智def";
//		  String str1 = "我ABC汉";
//		  int num = trimGBK(str.getBytes("GBK"), 4);
//		  System.out.println(str.substring(0, num));
//		  System.out.println("haha"+(int)'0');
		System.out.println(-10%3);

	}

	private static void aa() {
		String s="我ABC汉DEF";
		char[] strChar = s.toCharArray();
		System.out.println(strChar.length);
		int y=1;
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<y;i++){
			sb.append(strChar[i]);
		}
		System.out.println(sb.toString());
	}
	public static int trimGBK(byte[] buf, int n) {
		  int num = 0;
		  boolean bChineseFirstHalf = false;
		  for (int i = 0; i < n; i++) {
		   if (buf[i] < 0 && !bChineseFirstHalf) {
		    bChineseFirstHalf = true;
		   } else {
		    num++;
		    bChineseFirstHalf = false;
		   }
		  }
		  return num;
		 }
}
